#!/usr/bin/python
# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = [
           u'FileWriter',
           u'LogWriter',
           u'StdTerminalWriter',
           u'NullWriter'
           ]

import sys

from _pylot.minimal.system import ENCODING
from _pylot.minimal.utils import (check_type, check_value)
from _pylot.minimal.default.impl_streamwriter import StreamWriter

class NullWriter (StreamWriter):
  def __init__ (self):
    StreamWriter.__init__(self)
  def _write (self, text, style):
    return

class StdTerminalWriter (StreamWriter):

  def __init__ (self, stream=sys.stdout):
    super(StreamWriter, self).__init__()
    check_value(u'stream', stream, [sys.stdout, sys.stderr])
    self.stream = stream

  @classmethod
  def formatText (cls, text, style):
    if text is None :
      return ''

    if isinstance(text, unicode) :
      text = text.encode(ENCODING, u'replace')
    else :
      text = str(text)

    if style == u'title' :
      line = '-' * min(len(text), 80)
      text = '\n%s\n%s\n%s\n' % (line, text, line)
      text = '\033[1;34m%s\033[0;0m' % text
    elif style == u'warn':
      text = '\033[1;31m%s\033[0;0m' % text
    elif style == u'info':
      text = '\033[1;30m%s\033[0;0m' % text
    else :
      pass
    return text

  def _write (self, text, style):
    self.stream.write(self.formatText(text, style))
    self.stream.flush()

class FileWriter (StreamWriter):

  def __init__ (self, open_file):
    StreamWriter.__init__(self)
    self.stream = open_file

  def _write (self, text, style):
    self.stream.write(text)

class LogWriter (StreamWriter):

  def __init__ (self, open_file):
    super(LogStreamWriter, self).__init__()
    self.stream = open_file

  @classmethod
  def formatText (cls, text, style):
    if text is None :
      return ''

    if isinstance(text, unicode) :
      text = text.encode(ENCODING, u'replace')
    else :
      text = str(text)

    if style == u'title' :
      line = '*' * 80
      text = '%s\n%s\n%s\n' % (line, text, line)
    elif style == u'warn':
      text = '*** %s ***' % text
    elif style == u'info':
      text = '# %s' % text
    else :
      pass
    return text

  def _write (self, text, style):
    self.stream.write(self.formatText(text, style))
